package leetcode;

/**
 * @Classname DeleteDuplicates_0083
 * @Author happytaohaha
 * @Date 2020/3/8
 * @Description 删除排序链表中的重复元素
 */
public class DeleteDuplicates_0083 {
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode temp = head;
        int value = head.val;
        while (temp.next != null) {
            //1.只会更新下一个的值
            if (temp.next.val == value) {
                temp.next = temp.next.next;
            } else {
                //到下一个节点，也更换value 的值
                temp = temp.next;
                value = temp.val;
            }

        }
        return head;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode(1);
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode.next = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        deleteDuplicates(listNode);
    }


}
